﻿#include "nettools.h"
#include <QSettings>
#include <QJsonObject>
#include <QJsonDocument>


NetTools::NetTools(QObject *parent) : QObject(parent)
{

}

void NetTools::bindCloudAccount(const QString &account,const QString &server,const QString &code)
{
    //绑定云账号
    //发送post请求
    QNetworkRequest request;
    QNetworkAccessManager *naManager = new QNetworkAccessManager(this);
    QMetaObject::Connection connRet = QObject::connect(naManager,SIGNAL(finished(QNetworkReply*)),
                                                       this,SLOT(bindCloudAccountFinished(QNetworkReply*)));
    Q_ASSERT(connRet);

    request.setUrl(QUrl("http://"+server+":8080/onepassx/user/bindingAccount"));
    QByteArray postData;
    postData.append("u_username=").append(account).append("&").append("u_machine_code=").append(code);

    naManager->post(request,postData);
}

void NetTools::testServer(const QString &server)
{
    QNetworkRequest request;
    QNetworkAccessManager *naManager = new QNetworkAccessManager(this);
    QMetaObject::Connection connRet = QObject::connect(naManager,SIGNAL(finished(QNetworkReply*)),
                                                       this,SLOT(testServerFinished(QNetworkReply*)));
    Q_ASSERT(connRet);

    request.setUrl(QUrl("http://"+server+":8080/onepassx/user/sayHello"));
    QByteArray postData;
    postData.append("u_username=");

    naManager->post(request,postData);
}

void NetTools::bindCloudAccountFinished(QNetworkReply *r)
{
    //获取http状态码
    QVariant statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if(statusCode.isValid()){
        qDebug()<<"status code:"<<statusCode.toInt();
    }
    QVariant reason = r->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    if(reason.isValid()){
        qDebug()<<"reason="<<reason;
    }
    QNetworkReply::NetworkError err = r->error();
    if(err!=QNetworkReply::NoError){
        qDebug()<<"Failed: "<<r->errorString();
        emit bindCloudAccountResult(false,"Failed: "+r->errorString());
    }else{
        QSettings settings;
        //获取返回内容
        QString retStr = r->readAll();
        qDebug()<<retStr;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(retStr.toUtf8());
        if(jsonDoc.isObject()){
            QJsonObject jo = jsonDoc.object();
            if(jo.value("flag").toBool()){
                qDebug()<<"绑定成功!";
                //settings.setValue("cloud/activation","Y");
                //settings.setValue("cloud/account",ui->lineEditAccount->text().trimmed());


                if(jo.value("newUser").toBool()){
                    //是新注册的用户，全量同步本地数据过去

                }

                emit bindCloudAccountResult(true);
            }else{
                qDebug()<<jo.value("msg").toString();
                emit bindCloudAccountResult(false,jo.value("msg").toString());
            }
        }
    }
}

void NetTools::testServerFinished(QNetworkReply *r)
{
    //获取http状态码
    QVariant statusCode = r->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if(statusCode.isValid()){
        qDebug()<<"status code:"<<statusCode.toInt();
    }
    QVariant reason = r->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    if(reason.isValid()){
        qDebug()<<"reason="<<reason;
    }
    QNetworkReply::NetworkError err = r->error();
    if(err!=QNetworkReply::NoError){
        qDebug()<<"Failed: "<<r->errorString();
        emit testServerResult(false,"Failed: "+r->errorString());
    }else{
        //获取返回内容
        QString retStr = r->readAll();
        qDebug()<<retStr;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(retStr.toUtf8());
        if(jsonDoc.isObject()){
            QJsonObject jo = jsonDoc.object();
            if(jo.value("flag").toBool()){
                emit testServerResult(true);
            }else{
                emit testServerResult(false,"测试失败！");
            }
        }
    }
}
